package cn.mars4j.nutch.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.mars4j.nutch.web.bean.Metadata;
import cn.mars4j.nutch.web.form.SpiderForm;
import cn.mars4j.nutch.web.parser.CnkiParser;
import cn.mars4j.nutch.web.parser.Parser;
import cn.mars4j.nutch.web.parser.WanfangParser;

@Controller
public class SpiderController {
	
	
	
    @RequestMapping(value="spider")
    public ModelAndView spider(HttpServletRequest request,HttpServletResponse response, SpiderForm form) {
    	
        Map<Object, Object> context = new HashMap<Object, Object>();
        context.put(Parser.MKEY, form.getMkey());
        context.put(Parser.CLASSIFY, form.getClassify());
        
        
        Parser spider = new WanfangParser(context);
        
        if ("cnki".equals(form.getEngine())) {
        	spider = new CnkiParser(context);
        }
        try {
        	form.setResultList(spider.extract());;
        } catch(Exception e) {
        	e.printStackTrace();
        }
        
        ModelAndView mv = new ModelAndView("/spider/spider", "form" , form);

        return mv;
    }
}
